package com.duan.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.duan.dao.Daq_dbmsDao;
import com.duan.model.Datainfo;
import com.duan.util.Daq_dbmsUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class DatainfoGetForPDTChartServlet extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("utf-8");
		String nodeId = request.getParameter("nodeId"
				);
		Daq_dbmsDao dao = new Daq_dbmsDao();
		System.out.print(nodeId);
		String Descs[] = nodeId.split("_");
		if (Descs.length == 3) {
			int D_Source = Integer.parseInt(Descs[2]);
			List<Datainfo> list = dao.Source_20(D_Source);

			
			JSONArray chartdata = new JSONArray();
			JSONObject cdata = new JSONObject();

			for (int i = 0; i < list.size(); i++) {
				Datainfo data = (Datainfo) list.get(i);
				String v = new String();
				if(D_Source>5 && D_Source<15){
					v = String.valueOf(data.getD_Value()*2);
				}else{
					v = String.valueOf(data.getD_Value());
				}
				System.out.println(data.getD_Time().toString());
				cdata.put("time", data.getD_Time().toString().substring(11, 19));
				cdata.put("value", v);
				chartdata.add(cdata);
			}
			System.out.println(chartdata);
			PrintWriter out = response.getWriter();
			out.println(chartdata); 
		}

	}
}
